package org.microservice.system.controller;

import org.microservice.common.base.BaseControllerImpl;
import org.microservice.common.bean.DepartmentVo;
import org.microservice.common.util.R;
import org.microservice.system.dao.DepartmentDao;
import org.microservice.system.service.DepartmentService;
import org.microservice.system.utils.dto.DepartmentTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/7/11
 * Time: 16:05
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseControllerImpl<DepartmentDao,DepartmentVo> {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询部门list含有分页
     * @param params
     * @return
     */
    public R getListPage(@RequestBody(required = false) Map<String,Object> params){
        List<DepartmentTree> departmentTrees=  departmentService.getDepartmentListPage(params);
        return R.ok().put("data",departmentTrees);
    }



    public R save(@RequestBody DepartmentVo departmentVo){
        R r =departmentService.save(departmentVo);
        return r;
    }

    public R update(@RequestBody DepartmentVo departmentVo){
        R r =departmentService.update(departmentVo);
        return r;
    }

    /**
     * 由部门id获得子部门
     * @param parentId
     * @return
     */
    @GetMapping("/getChildrenDepartmentByparentId")
    public R getChildrenDepartmentByparentId(@RequestParam(value = "parentId",required = true) int parentId){
        List<DepartmentVo> departmentVos =departmentService.getChildrenDepartmentByparentId(parentId);
        return R.ok().put("data",departmentVos);
    }

    public R getById(@RequestParam("id") Serializable id){
        DepartmentVo departmentVo = departmentService.getDepartmentInfoById(id);
        return R.ok().put("data",departmentVo);
    }

    /**
     * 禁用启用部门
     * @param id
     * @param disabled
     * @return
     */
    @GetMapping("/disabledOrEnabled")
    public R disabledOrEnabled(@RequestParam(value = "id",required = true) int id,@RequestParam(value = "disabled",required = true) boolean disabled){
        departmentService.disabledOrEnabledById(id,disabled);
        return R.ok();
    }

}
